package com.a918.service.impl;

import com.a918.dao.*;
import com.a918.entity.*;
import com.a918.service.ImportDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ImportDataServiceImp implements ImportDataService {

    private OrgmonthMapper orgmonthMapper;
    private OrgdailyMapper orgdailyMapper;
    private DlvydailyMapper dlvydailyMapper;
    private DlvymonthMapper dlvymonthMapper;
    private WeightMapper weightMapper;
    @Autowired
    public ImportDataServiceImp(OrgmonthMapper orgmonthMapper,OrgdailyMapper orgdailyMapper,
                                DlvymonthMapper dlvymonthMapper,DlvydailyMapper dlvydailyMapper,
                                WeightMapper weightMapper) {
        this.orgmonthMapper = orgmonthMapper;
        this.orgdailyMapper = orgdailyMapper;
        this.dlvymonthMapper=dlvymonthMapper;
        this.dlvydailyMapper=dlvydailyMapper;
        this.weightMapper=weightMapper;
    }

    @Override
    public void insertOrgMonth(Orgmonth orgmonth) {
        orgmonthMapper.insert(orgmonth);
    }

    @Override
    public void insertOrgDaily(Orgdaily orgdaily) { orgdailyMapper.insert(orgdaily);
    }

    @Override
    public void insertDlvyMonth(Dlvymonth dlvymonth) { dlvymonthMapper.insert(dlvymonth);
    }

    @Override
    public void insertDlvyDaily(List<Dlvydaily> dlvydailyList) { dlvydailyMapper.insert(dlvydailyList);
    }

    @Override
    public void insertWeight(Weight weight) {
        weightMapper.insert(weight);
    }

    @Override
    public List<String> checkyuefen() {
        return weightMapper.checkyuefen();
    }

    @Override
    public void updateByYuefen(Weight weight) {
        weightMapper.updateByYuefen(weight);
    }

    @Override
    public List<Orgmonth> selectAll() {
        return orgmonthMapper.selectAll();
    }

    @Override
    public void rankByYuefen(String yuefen) {
        // 对指标和熵权法进行排名，分别为全部排名和同类排名
        orgmonthMapper.rankByYuefen(yuefen, "shangquanf", "rank1");

        String[] types = new String[] {"小规模", "中规模", "大规模" ,"超大规模"};
        for (String type : types) {
            orgmonthMapper.rankByYuefenAndType(yuefen, "shangquanf", "rank2", type);
            orgmonthMapper.rankByYuefenAndType(yuefen, "age_score", "age_rank", type);
            orgmonthMapper.rankByYuefenAndType(yuefen, "daily_score", "daily_rank", type);
            orgmonthMapper.rankByYuefenAndType(yuefen, "max_score", "max_rank", type);
            orgmonthMapper.rankByYuefenAndType(yuefen, "labor_score", "labor_rank", type);
            orgmonthMapper.rankByYuefenAndType(yuefen, "staff_score", "staff_rank", type);
            orgmonthMapper.rankByYuefenAndType(yuefen, "load_score", "load_rank", type);
        }
    }

    @Override
    public void updateRank(String yuefen) {
        // 添加等级
        int total = orgmonthMapper.getCountByYuefen(yuefen, null);
        orgmonthMapper.updateRank(yuefen, total, (int) (total * 0.75), "C");
        orgmonthMapper.updateRank(yuefen, (int) (total * 0.75), (int) (total * 0.25), "B");
        orgmonthMapper.updateRank(yuefen, (int) (total * 0.25), 0, "A");

        String[] types = new String[] {"小规模", "中规模", "大规模" ,"超大规模"};
        for (String type : types) {
            total = orgmonthMapper.getCountByYuefen(yuefen, type);
            orgmonthMapper.updateRankByType(yuefen, total, (int) (total * 0.75), type,"C");
            orgmonthMapper.updateRankByType(yuefen, (int) (total * 0.75), (int) (total * 0.25), type,"B");
            orgmonthMapper.updateRankByType(yuefen, (int) (total * 0.25), 0, type, "A");
        }
    }

}
